//
//  WssEngine.swift
//  MobileRTCSwiftSample
//
//  Created by 张衡 on 2019/11/7.
//  Copyright © 2019 Zoom Video Communications, Inc. All rights reserved.
//

import Foundation

class WssEngine:NSObject  {
    
    static let shared = WssEngine()
    
    func connect() {
        self.socket.delegate = self
        self.socket.connect()
    }
    
    func disconnect() {
        self.socket.disconnect()
    }
    
    lazy var socket: WebSocket = {
        let sock = WebSocket(url: URL(string: "wss://talk.1meeting.com:9501")!)
        return sock
    }()
    
    
    
}

extension WssEngine:WebSocketDelegate {
    func websocketDidConnect(socket: WebSocketClient) {
    }
    
    func websocketDidDisconnect(socket: WebSocketClient, error: Error?) {
        
    }
    
    func websocketDidReceiveMessage(socket: WebSocketClient, text: String) {
        
        guard let mode = WebSocketMode.deserialize(from: text.toDict()) else {
            return
        }
        
        switch mode.cmd {
        case 101:
            guard let quid = mode.questionid else {
                return
            }
            print("quid=\(quid)")
            NotificationCenter.default.post(name: MTReceiveQustionNotification, object: text)
            
        default:
            break
        }
    }
    
    func websocketDidReceiveData(socket: WebSocketClient, data: Data) {
    }
    
    
}
